package Controller.DepServlet;

import Dao.DepDao;
import Dao.StudentDao;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/dep_delete")
public class dep_delete extends HttpServlet {
    DepDao depDao = new DepDao();
    StudentDao studentDao = new StudentDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String dno = req.getParameter("dno");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        if (studentDao.selectByDno(dno).size() > 0) {
            map.put("code", 400);
            map.put("msg", "该系下有学生，不能删除");
        } else {
            depDao.delete(dno);
            map.put("msg", "删除成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
